﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Data.Models;

namespace Data.Sources
{
    public class File: IDataSource
    {
        private string _path;

        public File(string path)
        {
            _path = path;
        }

        public DataModels GetData()
        {
            DataModels data;
            FileStream fStream = new FileStream(_path, FileMode.Open);

            using (fStream)
            {
                BinaryFormatter bFormatter = new BinaryFormatter();
                data = (DataModels)bFormatter.Deserialize(fStream);
            }

            return data;
        }

        public void SaveData(DataModels data)
        {
            FileStream fStream = new FileStream(_path, FileMode.Create);

            using (fStream)
            {
                BinaryFormatter bFormatter = new BinaryFormatter();
                bFormatter.Serialize(fStream, data);
            }      
        }
    }
}
